﻿using Apps.Common;
using Apps.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Apps.BLL.RC
{
    public class RCSAMPLINGBLL
    {
        public DBContainer db = new DBContainer();


        public List<V_RC_SAMPLING> GetPagerList(ref GridPager pager, Expression<Func<V_RC_SAMPLING, bool>> whereLambda)
        {
            IQueryable<V_RC_SAMPLING> queryData = this.db.V_RC_SAMPLING.AsQueryable<V_RC_SAMPLING>();

            queryData = queryData.Where(whereLambda.Compile()).AsQueryable();
            pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            //排序
            return queryData.ToList();
        }

        public List<V_RC_SAMPLING> GetList(Expression<Func<V_RC_SAMPLING, bool>> whereLambda)
        {
            IQueryable<V_RC_SAMPLING> queryData = this.db.V_RC_SAMPLING.AsQueryable<V_RC_SAMPLING>();
            queryData = queryData.Where(whereLambda.Compile()).AsQueryable();
            return queryData.OrderByDescending(m => m.SAMPLING_BEGIN_TIME).ToList();
        }

        public List<SamplingRateModel> GetBLOCKList(string MATERIEL_ID, string SAMPLING_USER_ID, string SUPPLIER_ID, string StatisticalMethod, string SAMPLING_BEGIN_TIME, string SAMPLING_END_TIME)
        {
            List<SamplingRateModel> SamplingRateModelList = new List<SamplingRateModel>();
            string subtext = "";
            var data = (from m in db.RC_SAMPLING_POINT
                        join m_1 in db.SysUser on m.RC_SAMPLING.SAMPLING_USER_ID equals m_1.Id into joindata1
                        from m1 in joindata1.DefaultIfEmpty()
                        select new
                        {
                            SAMPLING_POINT = m,
                            SysUser = m1
                        }
                ).AsQueryable();

            if (!string.IsNullOrEmpty(MATERIEL_ID) && MATERIEL_ID != "-1")
            {
                data = data.Where(m => m.SAMPLING_POINT.RC_SAMPLING.RC_PROCESS.PUB_MATERIEL.MATERIEL_ID == MATERIEL_ID);
            }

            if (!string.IsNullOrEmpty(SAMPLING_USER_ID) && SAMPLING_USER_ID != "-1")
            {
                data = data.Where(m => m.SysUser.Id == SAMPLING_USER_ID);
            }

            if (!string.IsNullOrEmpty(SUPPLIER_ID) && SUPPLIER_ID != "-1")
            {
                data = data.Where(m => m.SAMPLING_POINT.RC_SAMPLING.RC_PROCESS.PUB_SUPPLIER.SUPPLIER_ID == SUPPLIER_ID);
            }

            if (!string.IsNullOrEmpty(SAMPLING_BEGIN_TIME))
            {
                DateTime STime = Convert.ToDateTime(SAMPLING_BEGIN_TIME);
                data = data.Where(m => m.SAMPLING_POINT.RC_SAMPLING.SAMPLING_BEGIN_TIME >= STime);
                subtext = SAMPLING_BEGIN_TIME;
            }

            if (!string.IsNullOrEmpty(SAMPLING_END_TIME))
            {
                DateTime ETime = Convert.ToDateTime(SAMPLING_END_TIME).AddDays(1).Date;
                data = data.Where(m => m.SAMPLING_POINT.RC_SAMPLING.SAMPLING_BEGIN_TIME <= ETime);
                subtext += (subtext == "" ? SAMPLING_END_TIME : " ~ " + SAMPLING_END_TIME);
            }
            SamplingRateModel mSamplingRateModel = new SamplingRateModel();

            var Data = (from p in data
                        group p by p.SAMPLING_POINT.BLOCK_NO into g1
                        orderby g1.Key ascending
                        select new
                        {
                            BLOCK_NO = g1.Key,
                            Count = g1.Count(),
                        }).ToList();
            if (Data.Count > 0)
            {
                List<string> seriesdata = new List<string>();
                for (int i = 1; i <= 18; i++)
                {
                    var bList = Data.Where(m => m.BLOCK_NO == i).ToList();

                    var BLOCK_NOData = bList.Count > 0 ? bList[0].Count : 0;
                    var mBLOCK_NO = mSamplingRateModel.GetType().GetProperty("BLOCK" + i);

                    mBLOCK_NO.SetValue(mSamplingRateModel, BLOCK_NOData);
                    seriesdata.Add(BLOCK_NOData.ToString());
                }

                var option = new
                {
                    title = new { text = "入厂采样分布率统计" },
                    tooltip = new { show = true, formatter = "第{b}块<br/>出现{c}次" },
                    legend = new { data = new string[] { "出现次数" } },
                    xAxis = new { name = "块号", type = "category", data = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 } },
                    yAxis = new { name = "次数", type = "value" },
                    series = new List<Models.Common.ChartSeriesModel>() { new Models.Common.ChartSeriesModel { name = "出现次数", type = "line", data = seriesdata } }
                };
                mSamplingRateModel.option = option;
                SamplingRateModelList.Add(mSamplingRateModel);
            }
            return SamplingRateModelList;
        }

        public List<SampleInfoModel> GetSampleInfo(ref GridPager pager, string BATCH_CODE, string SAMPLING_BEGIN_TIME, string SAMPLING_END_TIME,string MATERIEL_NAME="")
        {
            MATERIEL_NAME = MATERIEL_NAME.Contains("==") ? "" : MATERIEL_NAME;
            var Data = db.V_RC_SAMPLING.AsQueryable();
            Data = Data.Where(m => m.ISERROR != true);
            if (!string.IsNullOrEmpty(BATCH_CODE))
            {
                Data = Data.Where(m => m.BATCH_CODE.IndexOf(BATCH_CODE) >= 0);
            }
            if (!string.IsNullOrEmpty(MATERIEL_NAME))
            {
                Data = Data.Where(m => m.MATERIEL_NAME.IndexOf(MATERIEL_NAME) >= 0);
            }

            if (!string.IsNullOrEmpty(SAMPLING_BEGIN_TIME))
            {
                DateTime STime = Convert.ToDateTime(SAMPLING_BEGIN_TIME);
                Data = Data.Where(m => m.SAMPLING_BEGIN_TIME >= STime);
            }

            if (!string.IsNullOrEmpty(SAMPLING_END_TIME))
            {
                DateTime ETime = Convert.ToDateTime(SAMPLING_END_TIME).AddDays(1);
                Data = Data.Where(m => m.SAMPLING_END_TIME <= ETime);
            }
            var SData = from p in Data
                        group p by new { p.BATCH_CODE, p.SAMPLE_PAIL, p.RFID ,p.SAMPLING_NODE_NAME,p.SUPPLIER_NAME,p.MATERIEL_NAME} into g1
                        select new SampleInfoModel
                        {
                            BATCH_CODE = g1.Key.BATCH_CODE,
                            SAMPLE_PAIL = g1.Key.SAMPLE_PAIL,
                            SAMPLE_CODE = g1.Key.RFID,
                            SAMPLING_NODE_NAME=g1.Key.SAMPLING_NODE_NAME,
                            SUPPLIER_NAME=g1.Key.SUPPLIER_NAME,
                            MATERIEL_NAME = g1.Key.MATERIEL_NAME,
                            SAMPLING_END_TIME = g1.Max(m => m.SAMPLING_END_TIME),
                            SCount = g1.Count()
                        };
            pager.totalRows = SData.Count();
            SData = LinqHelper.SortingAndPaging(SData, pager.sort, pager.order, pager.page, pager.rows);
            return SData.OrderByDescending(m => m.BATCH_CODE).ThenBy(m => m.SAMPLE_PAIL).ToList();
        }

        public List<PUB_SAMPLE_PAIL_WEIGHT> GetSampleWeight(string SAMPLING_NODE_ID, string SAMPLING_TIME, int type = 0)
        {
            DateTime STime = Convert.ToDateTime(SAMPLING_TIME).Date;
            DateTime ETime = Convert.ToDateTime(SAMPLING_TIME).AddDays(1).Date;
            IQueryable<PUB_SAMPLE_PAIL_WEIGHT> resultData = null;
            if (type == 0)
            {
                IQueryable<PUB_SAMPLE_PAIL_WEIGHT> queryData = this.db.PUB_SAMPLE_PAIL_WEIGHT.AsQueryable();
                resultData = queryData.Where(m => m.SAMPLING_NODE_ID == SAMPLING_NODE_ID && m.TIME >= STime && m.TIME <= ETime).AsQueryable();
            }
            else
            {
                IQueryable<PUB_SAMPLE_PAIL_WEIGHTS> queryData = this.db.PUB_SAMPLE_PAIL_WEIGHTS.AsQueryable();
                return TransHelper.TransExpList<PUB_SAMPLE_PAIL_WEIGHTS, PUB_SAMPLE_PAIL_WEIGHT>(queryData.Where(m => m.SAMPLING_NODE_ID == SAMPLING_NODE_ID && m.TIME >= STime && m.TIME <= ETime && m.WEIGHT > 0).AsQueryable().ToList());
            }
            return resultData.OrderBy(m => m.TIME).ToList();
        }


        public List<PUB_SAMPLE_PAIL_WEIGHTS> GetChildWeight(string SAMPLING_NODE_ID, string SAMPLING_TIME)
        {
            DateTime STime = Convert.ToDateTime(SAMPLING_TIME).Date;
            DateTime ETime = Convert.ToDateTime(SAMPLING_TIME).AddDays(1).Date;
            IQueryable<PUB_SAMPLE_PAIL_WEIGHTS> resultData = null;
            IQueryable<PUB_SAMPLE_PAIL_WEIGHTS> queryData = this.db.PUB_SAMPLE_PAIL_WEIGHTS.AsQueryable();
            resultData = queryData.Where(m => m.SAMPLING_NODE_ID == SAMPLING_NODE_ID && m.TIME >= STime && m.TIME <= ETime && m.WEIGHT > 0).AsQueryable();
            return resultData.OrderBy(m => m.TIME).ToList();
        }
    }
}
